package wfmanage.action;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import edu.wfmanage.models.HelpDeskUser;
import edu.wfmanage.models.Ticket;

import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

public class SupportExecutiveViewTicket extends ActionSupport {
  
	private int id;
	private String summary = "";
	private String category = "";
	private String device = "";
	private String os = "";
	private Date date;
	private String priority = "";
	private String cust_name = "";
	private String email = "";
	private String dept = "";
	private String office = "";
	private String phone = "";
	private String desc = "";
	private String comments = "";
	private ArrayList<Ticket> tickets;
	private ArrayList<String> names;
	private String name;
	private String nextSupportLevel;
	private int status;
		
	private String rolename;
	private String commandbutton = "";
	
	private ArrayList<HelpDeskUser> userlist = new ArrayList<HelpDeskUser>();
	
	public String execute(){
		
		Ticket t = Ticket.findTicket("where t.ticket_id= " + getId() + ";");
		
		MyLog.log("inside SupportExecutiveViewTicket.execute()"+id);
		this.category = t.getCategory();
		
        this.cust_name = t.getCust_name();
		this.date = t.getDate();
		this.dept = t.getDept();
		this.desc = t.getDesc();
		this.device = t.getDevice();
		this.email = t.getEmail();
		this.office = t.getOffice();
		this.os = t.getOs();
		this.phone = t.getPhone();
		this.priority = t.getPriority();
		this.status = t.getStatus_id();
		this.summary = t.getSummary();
		
		rolename = SupportExecutiveViewTicket.findRoleName(status);
		
		
		if(this.commandbutton.equalsIgnoreCase("SubmitResponse")){
			MyLog.log("SupportExecutive comments are:"+comments);			
			Ticket.updateComments(id, getComments());
			Ticket.updateStatus(id, 5);
			userlist = HelpDeskUser.findUserList("HelpDesk_User");
			email mail=new email();
	        for(HelpDeskUser u: userlist){
	             MyLog.log("user is "+u.getEmailid());
	             mail.sendmail(u.getUsername(),u.getEmailid(),"HelpDesk_User",0, getComments());
	             MyLog.log("An email has been sent to "+u.getEmailid());
	        }
			return "submit_response";
		}
		
		if(this.commandbutton.startsWith("Sign")){
			return "sign_out";
		}
		if(this.commandbutton.startsWith("Forward")){
			Ticket.updateComments(id, getComments());
			String nextSupport = Ticket.findNextSupportLevel(status);
			this.setNextSupportLevel(nextSupport);
			names = ForwardToNextLevel.findStrings(nextSupport);

			return "forward_ticket";
		}
		
		return "initial_entry";
	}
	
	public static String findRoleName(int status_id){
		
		String role="";
		ResultSet result = null;
		Connection con = DB.getConnection();
		String query = "select role.role_name as role_name from role, assignment "
				+ "where role.role_id = assignment.support_id and assignment.status_id = " + status_id
				+ " ";
		result = DB.readFromDB(query, con);
		try{
		 while(result.next()){
			 role = result.getString("role_name");
		 }
		}catch(SQLException ex){
			ex.printStackTrace();
		}
		
		return role;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getDevice() {
		return device;
	}

	public void setDevice(String device) {
		this.device = device;
	}

	public String getOs() {
		return os;
	}

	public void setOs(String os) {
		this.os = os;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getPriority() {
		return priority;
	}

	public void setPriority(String priority) {
		this.priority = priority;
	}

	public String getCust_name() {
		return cust_name;
	}

	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public String getOffice() {
		return office;
	}

	public void setOffice(String office) {
		this.office = office;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public ArrayList<Ticket> getTickets() {
		return tickets;
	}

	public void setTickets(ArrayList<Ticket> tickets) {
		this.tickets = tickets;
	}

	public ArrayList<String> getNames() {
		return names;
	}

	public void setNames(ArrayList<String> names) {
		this.names = names;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNextSupportLevel() {
		return nextSupportLevel;
	}

	public void setNextSupportLevel(String nextSupportLevel) {
		this.nextSupportLevel = nextSupportLevel;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getCommandbutton() {
		return commandbutton;
	}

	public void setCommandbutton(String commandbutton) {
		this.commandbutton = commandbutton;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	public ArrayList<HelpDeskUser> getUserlist() {
		return userlist;
	}

	public void setUserlist(ArrayList<HelpDeskUser> userlist) {
		this.userlist = userlist;
	}
	
}
